import { getCompetitionList, newCompetition, setCurrentCompetition, 
  getCurrentCompetitionDetail, getCompetitionDetail, publishCompetition, enrollCompetition,
  getEnrollList, getCompetitionReviewList, getEnrollUser, setGradeTeacher, getGradeTeacher, getCharts,
  endCompetition, reEditCompetition, getTeacherGrade, deleteCompetition } from '@/services/competition'
  import { stringify } from 'qs';

export default {
  namespace: 'competition',

  state: {
    publicCompetitionList: [],
    unpublicCompetitionList: [],
    endCompetitionList: [],
    competitionDetail: {},
    competition:{},
    enrollList:[],
    reviewList:[],
    enrollUser:[],
    gradeTeacher:[],
    charts:[],
    teacherGrade:[],
  },

  effects: {
    *getCompetitionDetail({ payload, callback },{ call, put }){
      const response = yield call(getCompetitionDetail, payload)
      const competition = response.data;
      // 单项选择
      const singleLevelOptionList = [];
      // 双项选择
      const doubleLevelOptionList = [];

      competition.singleLevelOptionVo.forEach(element => {
        const singleLevelOption = element
        const option = element.option.split(",");
        singleLevelOption["option"] = option;
        singleLevelOptionList.push(singleLevelOption)
      });

      competition.doubleLevelOptionVo.forEach(element => {
        const firstOption = element.firstOption.split(",")
        const secondOption = element.secondOption.split(":")
        const tempList = []
        const doubleLevelOption = {}
        for(var i=0;i<firstOption.length;i++){
          const option = {
            value: "",
            label: "",
            children: [],
          }
          option["value"] = firstOption[i]
          option["label"] = firstOption[i]
          const temp = secondOption[i].split(",")
          temp.forEach(element => {
            const e = {}
            e["value"] = element
            e["label"] = element
            option["children"].push(e)
          }) 
          tempList.push(option) 
        }
        doubleLevelOption["id"] = element.id
        doubleLevelOption["title"] = element.title
        doubleLevelOption["option"] = tempList

        doubleLevelOptionList.push(doubleLevelOption)
      });


      competition.singleLevelOptionVo=singleLevelOptionList;
      competition.doubleLevelOptionVo=doubleLevelOptionList;

      yield put({
        type: 'saveCompetition',
        payload: competition
      })
    },
    *getPublicCompetitionList({ payload, callback },{ call, put }){
      const response = yield call(getCompetitionList, payload)
      // console.log("model",response)
      yield put({
        type: 'setPublicCompetitionList',
        payload: response.data,
      })
    },
    *getUupublicCompetitionList({ payload, callback },{ call, put }){
      const response = yield call(getCompetitionList, payload)
      // console.log("model",response)
      yield put({
        type: 'setUupublicCompetitionList',
        payload: ['',...response.data],
      })
      callback&&callback(['',...response.data])
    },
    *getEndCompetitionList({ payload, callback },{ call, put }){
      const response = yield call(getCompetitionList, payload)
      // console.log("model",response)
      yield put({
        type: 'setEndCompetitionList',
        payload: response.data,
      })
    },
    *newCompetition({ payload, callback },{ call, put }){
      const response = yield call(newCompetition, payload)
      callback&&callback(response.data)
    },
    *deleteCompetition({ payload, callback },{ call, put }){
      const response = yield call(deleteCompetition, payload)
      yield put({
        type: 'getUupublicCompetitionList',
        payload: {
          status: 0
        },
      })
      callback&&callback(response.data)
    },
    *setCurrentCompetition({ payload, callback },{ call, put }){
      const response = yield call(setCurrentCompetition, payload)
      callback&&callback(response.data)
    },
    *getCurrentCompetitionDetail(_,{ call, put }){
      const response = yield call(getCurrentCompetitionDetail)
      yield put({
        type: 'saveCompetitionDetail',
        payload: response.data
      })
    },
    *publishCompetition({ payload, callback },{ call, put }){
      const response = yield call(publishCompetition, payload)
      yield put({
        type: 'getPublicCompetitionList',
        payload: {
          status: 3
        }
      })
      yield put({
        type: 'getUupublicCompetitionList',
        payload: {
          status: 0
        },
      })
      callback&&callback(response.data);
    },
    *reEditCompetition({ payload, callback },{ call, put }){
      const response = yield call(reEditCompetition, payload)
      yield put({
        type: 'getPublicCompetitionList',
        payload: {
          status: 3
        }
      })
      yield put({
        type: 'getUupublicCompetitionList',
        payload: {
          status: 0
        },
      })
      callback&&callback(response.data);
    },
    *endCompetition({ payload, callback },{ call, put }){
      const response = yield call(endCompetition,payload);
      yield put({
        type: 'getPublicCompetitionList',
        payload: {
          status: 3
        }
      })
      yield put({
        type: 'getEndCompetitionList',
        payload: {
          status: 5
        },
      })
      callback&&callback(response.data);
    },
    *enrollCompetition({ payload, callback },{ call, put }){
      const response = yield call(enrollCompetition, payload)
      // const params = [];
      // console.log(payload)
      // // eslint-disable-next-line no-restricted-syntax
      // for (const key in payload) {
      //   if (payload.hasOwnProperty(key)) {
      //     const element = payload[key];
      //     console.log(key,element)
      //     const param = new Object();
      //     param.id = key;
      //     param.message = element;
      //     params.push(param);
      //   }
      // }
      // const paramss = new Object();
      // paramss.vo = params
      // const response = yield call(enrollCompetitionMessage, paramss)

      callback&&callback(response)
    },
    *getEnrollList({ payload, callback },{ call, put }){
      const response = yield call(getEnrollList, payload)
      yield put({
        type: 'saveEnrollCompetition',
        payload: response.data,
      })
    },
    *getCompetitionReviewList({ payload, callback },{ call, put }){
      const response = yield call(getCompetitionReviewList)
      yield put({
        type: 'saveReviewList',
        payload: response.data,
      })
    },
    *getEnrollUser({ payload, callback },{ call, put }){
      const response = yield call(getEnrollUser,payload);
      yield put({
        type: 'saveEnrollUser',
        payload: response.data,
      })
    },
    *setGradeTeacher({ payload, callback },{ call, put }){
      const response = yield call(setGradeTeacher,payload)
      callback&&callback(response.data)
    },
    *getGradeTeacher({ payload, callback },{ call, put }){
      const response = yield call(getGradeTeacher,payload)
      yield put({
        type: 'saveGradeTeacher',
        payload: response.data,
      })
    },
    *getCharts({ payload, callback },{ call, put }){
      const response = yield call(getCharts,payload)
      yield put({
        type: 'saveCharts',
        payload: response.data,
      })
    },
    *getTeacherGrade({ payload, callback },{ call, put }){
      const response = yield call(getTeacherGrade,payload);
      callback&&callback(response.data)
    }
    
  },

  reducers: {
    setPublicCompetitionList(state, action) {
      return {
        ...state,
        publicCompetitionList: action.payload,
      }
    },
    setUupublicCompetitionList(state, action) {
      return {
        ...state,
        unpublicCompetitionList: action.payload,
      }
    },
    setEndCompetitionList(state, action) {
      return {
        ...state,
        endCompetitionList: action.payload,
      }
    },
    saveCompetitionDetail(state, action) {
      return {
        competitionDetail: action.payload,
      }
    },
    saveCompetition(state, action) {
      return {
        competition: action.payload,
      }
    },
    saveEnrollCompetition(state, action) {
      return {
        enrollList: action.payload,
      }
    },
    saveReviewList(state, action) {
      return {
        reviewList: action.payload,
      }
    },
    saveEnrollUser(state, action){
      return {
        enrollUser: action.payload,
      }
    },
    saveGradeTeacher(state, action){
      return {
        gradeTeacher: action.payload,
      }
    },
    saveCharts(state, action){
      return {
        charts: action.payload,
      }
    },
    saveTeacherGrade(state, action){
      return {
        ...state,
        teacherGrade: action.payload,
      }
    }
  }
};
